

package com.bei_en.teacher.admin.service.regions;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.regions.bean.Regions;
import com.bei_en.teacher.admin.model.regions.vo.RegionsVo;
import com.bei_en.teacher.admin.model.regions.condition.RegionsCondition;
import com.bei_en.teacher.admin.persistence.dao.regions.RegionsDao;

/**
 * 
 * @author Guocg
 * @date 2016-10-3 23:27:08
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class RegionsService extends BaseService{

    @Autowired
    private RegionsDao regionsDao;

    public void insert(RegionsVo model) {

    	 regionsDao.insert(model.getRegions());
    }

//    /**
//     * 插入并返回生成的id
//     * @param model
//     * @return
//     */
//    public Integer merge(RegionsVo model) {
//
//	regionsDao.insert(model.getRegions());
//	return regionsDao.findLastInsertedId();
//
//    }

    public void delete(Integer modelInteger) {

    	regionsDao.delete(modelInteger);
    }

    public RegionsVo findById(Integer modelInteger) {

    	RegionsVo model = new RegionsVo(regionsDao.findById(modelInteger));
    	return model;
    }

    public void update(RegionsVo model) {

    	regionsDao.update(model.getRegions());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(RegionsVo model) {

    	regionsDao.updateSelective(model.getRegions());
    }

    public int countAll() {

    	return regionsDao.countAll();
    }

    public List<RegionsVo> findAll() {

		List<RegionsVo> list = new ArrayList<RegionsVo>();
		List<Regions> list1 = regionsDao.findAll();
		for (Regions model : list1) {
		    list.add(new RegionsVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return regionsDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<RegionsVo> findRegionsPage(RegionsCondition vo) {

		Integer totalCount = searchRegionsNum(vo);
		List<RegionsVo> list = new ArrayList<RegionsVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "order_by  desc,item_id  ");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<Regions> list1 = regionsDao.findPageBreakByCondition(vo, rb);
		    for (Regions model : list1) {
		    	list.add(new RegionsVo(model));
		    }
	
		}

		return list;
    }

    private Integer searchRegionsNum(RegionsCondition Vo) {

    	return regionsDao.findNumberByCondition(Vo);
    }

}
